<?php
namespace app\admin\controller;
use app\BaseController;
// use think\facade\Cache;

use app\common\model\PageModel;

class Page extends BaseController{
    public function list(){
        $where=[
            ['firm_id','=',$this->request->firm_id],
            ['is_delete','=',0]
        ];
        if(!empty($keyword)){
            $where[] = ['page_name','LIKE','%'.$keyword.'%'];
        }
        $list = PageModel::where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('sort','desc')->select();
        $total = PageModel::where($where)->count('page_id');
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    
    public function detail(){
        $page_id = $this->request->param('page_id/d',0);
        if(empty($page_id)){
            return error('错误操作');
        }
        if(null === $PageModel = PageModel::field('page_name,page,menu,menu_type,firm_id')->find($page_id)){
            return error('内容不存在');
        }
        // if($PageModel->firm_id != $this->request->firm_id){
        //      return error('没有权限');
        // }
        return success($PageModel);
    }
    
    public function save(){
        
        $page_id = $this->request->param('page_id/d',0);
        
        $page_name = $this->request->param('page_name/s','');
        
        $sort  = $this->request->param('sort/d',0);
        
        $page = $this->request->param('page/s','');
        
        $page = json_decode($page,true);
        
        $menu= $this->request->param('menu/s','');
        
        $menu = json_decode($menu,true);
        
        $menu_type = $this->request->param('menu_type/s','none');
        
        if(empty($page_id)){
            $PageModel = new PageModel();
            $PageModel->firm_id = $this->request->firm_id;
        }else{
            if(null === $PageModel = PageModel::find($page_id)){
                return error('错误操作');
            }
            if($PageModel->firm_id != $this->request->firm_id){
                 return error('请选择要编辑的内容');
            }
        }
        if($page){
            $PageModel->page = $page;
        }
        if($menu){
            $PageModel->menu = $menu;
            $PageModel->menu_type = $menu_type;
        }
        if($page_name){
            $PageModel->page_name = $page_name;
            $PageModel->sort = $sort;
            
        }
        $PageModel->save();
        return success($PageModel);
        
    }
    public function delete(){
        $page_id = $this->request->param('page_id/d',0);
        if(empty($page_id)){
            return error('错误操作');
        }
        if(null === $articleModel = PageModel::find($page_id)){
            return error('文章不存在');
        }
        if($articleModel->firm_id != $this->request->firm_id){
            return error('没有权限');
        }
        $articleModel->is_delete = 1;
        $articleModel->save();
        return success('删除成功');
    }
}